package com.klikmee.sms.chat.web.usecases;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;

import com.dotexe.webframework.core.ClientApplication;
import com.dotexe.webframework.core.clientactions.MessageBox;
import com.dotexe.webframework.core.datasources.CollectionDataSource;
import com.dotexe.webframework.core.triggers.Trigger;
import com.klikmee.sms.chat.web.dao.AgentDAO;
import com.klikmee.sms.chat.web.util.SpringBeansLookup;
import com.klikmee.sms.services.chat.model.Agent;

public class EmployeeListForm extends ClientApplication {

	private CollectionDataSource dsAgents;
	private ArrayList<String> bcAgents;

	private List<Agent> agents;
	private boolean _selectionMode;

	private EmployeeDetailsForm details;
	private AgentDAO agentDAO;

	// override
	@Override
	public void load() throws Exception {
		super.load();

		this.dsAgents = new CollectionDataSource("dsAgents");
		this.getDataSources().add(this.dsAgents);
		this.bcAgents = new ArrayList<String>();
		this.bcAgents.add("AgentFirstName");
		this.bcAgents.add("AgentLastName");
		this.bcAgents.add("AgentUserName");
		this.bcAgents.add("AgentPassword");
		this.bcAgents.add("AgentEmail");
		this.bcAgents.add("AgentPhone");
		this.bcAgents.add("Active");

		this.triggers.add(new Trigger(this, "btnSearchAgentClick"));
		this.triggers.add(new Trigger(this, "btnEditAgentClick"));
		this.triggers.add(new Trigger(this, "btnAddAgentClick"));
		this.triggers.add(new Trigger(this, "btnDeleteAgentClick"));
		this.triggers.add(new Trigger(this, "gvAgentsCellClick"));
		this.triggers.add(new Trigger(this, "gvAgentsCellDoubleClick"));
		this.triggers.add(new Trigger(this, "AppEmployeeListFormFormClosed"));
	}

	@Override
	public void start() throws Exception {
		super.start();
		bindAgentsList();
	}

	public void btnSearchAgentClick() {

	}

	public void btnEditAgentClick() throws InstantiationException, IllegalAccessException, JSONException, Exception {
		if (this.dsAgents.getSelectedItem() == null) {
			MessageBox.Show(this, "Obavestenje", "Morate selektovati agenta!");
			return;
		} else {
			details = createChildApplication(EmployeeDetailsForm.class);
			details.setAgent((Agent) this.dsAgents.getSelectedItem());
			details.triggers.get("btnSaveClick").addOnFire(this, "afterAgentSave");
			details.setEditAgent(true);
			this.showChildApp(details);
			details.start();
		}
	}

	public void btnAddAgentClick() throws InstantiationException, IllegalAccessException, JSONException, Exception {
		details = createChildApplication(EmployeeDetailsForm.class);
		details.setAgent(new Agent());
		details.triggers.get("btnSaveClick").addOnFire(this, "afterAgentSave");
		details.setEditAgent(false);
		this.showChildApp(details);
		details.start();
	}

	public void btnDeleteAgentClick() throws SecurityException, ClassNotFoundException, NoSuchFieldException, IllegalAccessException, JSONException, NoSuchMethodException, InvocationTargetException {
		if (this.dsAgents.getSelectedItem() == null) {
			MessageBox.Show(this, "Obavestenje", "Morate selektovati agenta!");
			return;
		} else {
			Agent agent = (Agent) this.dsAgents.getSelectedItem();
			agent.setActive(false);
			agentDAO.edit(agent);
			bindAgentsList();
		}
	}

	public void gvAgentsCellClick() {

	}

	public void gvAgentsCellDoubleClick() throws InstantiationException, IllegalAccessException, JSONException, Exception {
		if (_selectionMode) {
			// parent will handle this!
		} else {
			details = createChildApplication(EmployeeDetailsForm.class);
			details.setAgent((Agent) this.dsAgents.getSelectedItem());
			details.triggers.get("btnSaveClick").addOnFire(this, "afterAgentSave");
			details.setEditAgent(true);
			this.showChildApp(details);
			details.start();
		}
	}
	
	public void AppEmployeeListFormFormClosed() {
		
	}
	
	public void set_selectionMode(boolean _selectionMode) {
		this._selectionMode = _selectionMode;
	}

	public boolean get_selectionMode() {
		return _selectionMode;
	}

	public void afterAgentSave() throws SecurityException, ClassNotFoundException, NoSuchFieldException, IllegalAccessException, JSONException, NoSuchMethodException, InvocationTargetException {
		bindAgentsList();
	}

	private void bindAgentsList() throws SecurityException, ClassNotFoundException, NoSuchFieldException, IllegalAccessException, JSONException, NoSuchMethodException, InvocationTargetException {
		agentDAO = SpringBeansLookup.getBean(AgentDAO.class);
		agents = agentDAO.selectAll();
		this.dsAgents.setSource(agents);
		this.dsAgents.bind(bcAgents);
	}
}
